package indzcraft.ranking;

import indzcraft.loader.IndzCraftLoader;
import indzcraft.loader.ItemEquiped;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class Database {
	
	private HashMap<Integer,ItemEquiped> db = new HashMap<Integer,ItemEquiped>();
	IndzCraftLoader loader;

	public Database(IndzCraftLoader loader) {
		this.loader = loader;
		loadFromStorage();
	}
	
	public void loadFromStorage(){
		File file = new File("./wowhead/");
		if(file.exists()){
			System.out.println("Path /wowhead/ exists");
			File[] files = file.listFiles();
			for(int i = 0;i< files.length;i++){
				if(files[i].isFile()){
					int itemid = Integer.parseInt(files[i].getName().split("\\.")[0]);
					ItemEquiped item = loader.getItemFromWOWHEAD(itemid);
					db.put(item.getId(), item);
				}
			}
		}
		System.out.println("directory wowhead imported");
	}
	
	public void put(int id,ItemEquiped item){
		db.put(id, (ItemEquiped) item);
	}

	public ItemEquiped get(int id){
		if(!db.containsKey(id)){
			ItemEquiped item = loader.getItemFromWOWHEAD(id);
			db.put(item.getId(), item);
		}
		//System.out.println("load from db: "+ id);
		return (ItemEquiped) db.get(id);
	}
	
	// TODO dirty shit ey
	public ArrayList<ItemEquiped> findAllWithInventoryType(String slotName,Integer[] integers){
		Arrays.sort(integers);
		ArrayList<ItemEquiped> list = new ArrayList<ItemEquiped>();
		for(Integer i : db.keySet()){
			int it = db.get(i).getInventoryType();
			if(Arrays.binarySearch(integers,it) >= 0){
				if(slotName.equals("mainHand") && db.get(i).getSpeed() >= 1.8)
					list.add(db.get(i));
				else if(slotName.equals("offHand") && db.get(i).getSpeed() <= 1.8)
					list.add(db.get(i));
				else if(!slotName.equals("mainHand") && !slotName.equals("offHand"))
					list.add(db.get(i));
			}
		}
		return list;
	}
}
